﻿import ko = require("knockout");

class preload {
    public static load() {
        return WinJS.Promise.join([
            preload.loadSingle("TrackSettings"),
            preload.loadSingle("Knob"),
            preload.loadSingle("TrackVisual"),
            preload.loadSingle("Clip"),
            preload.loadSingle("Selection"),
            preload.loadSingle("Cursor"),
            preload.loadSingle("RenameTrackDialog"),
            preload.loadSingle("ClipTile"),
            preload.loadSingle("OpenProjectDialog"),
            preload.loadSingle("SaveProjectDialog")
        ]);
    }

    private static loadSingle(name: string) {
        return new WinJS.Promise((complete) => {
            var elementName = name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
            if (elementName.indexOf("-") == -1) elementName = "ctl-" + elementName;

            require(
                ["text!components/" + name + "/" + name + ".html", "components/" + name + "/" + name],
                (t, vm) => {
                    ko.components.register(elementName, { template: t, viewModel: vm, synchronous: true });
                    complete();
                }
            );
        });
    }
}

export = preload